
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.TelnetServer;
import com.crv.ci.service.ITelnetServerService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class TelnetServerAction extends AbstractAdminController<ITelnetServerService>{

    @Autowired
    private ITelnetServerService telnetServerService;
    
    @RequestMapping(value="/telnetserver/list")
    public String list(TelnetServer telnetServer,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(telnetServer); 	
    	BizData4Page<TelnetServer> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("telnetServer", telnetServer);
    	return "/module/telnetserver/list";
    }    
    
    @RequestMapping(value="/telnetserver/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("telnetServer", new TelnetServer());
        return "module/telnetserver/edit";
    }    
    
    @RequestMapping(value="/telnetserver/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TelnetServer telnetServer = telnetServerService.fetch(id);
    	model.addAttribute("telnetServer", telnetServer);
        return "module/telnetserver/edit";
    }    
    
    @RequestMapping(value="/telnetserver/save")
    @ResponseBody
    public String save(HttpServletRequest request,TelnetServer telnetServer){
		boolean isAddNew = StringUtils.isBlank(telnetServer.getId())?true:false;
		if(isAddNew)
		{
			telnetServer.setId(IdGenerator.createNewId());
			telnetServerService.add(telnetServer);
		}
		else
		{
			telnetServerService.update(telnetServer);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/telnetserver/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	telnetServerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(TelnetServer telnetServer)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("telnetAlias", telnetServer.getTelnetAlias());		
		conditions.put("groupId", telnetServer.getGroupId());		
		conditions.put("telnetHost", telnetServer.getTelnetHost());		
		conditions.put("telnetType", telnetServer.getTelnetType());		
		conditions.put("telnetUser", telnetServer.getTelnetUser());		
		conditions.put("telnetPass", telnetServer.getTelnetPass());		
		conditions.put("createDate", telnetServer.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ITelnetServerService getMainService() {
        return telnetServerService;
    }

    @Override
    protected String getMainObjName() {
        return "telnetserver";
    }

    @Override
    protected String getViewTitle() {
        return "telnetserver";
    }
}
